package com.firebroo.PullObserver;

import java.util.ArrayList;
import java.util.List;

public abstract class Subject {
    private List<Observer> list = new ArrayList<Observer>();

    public List<Observer> getList() {
        return list;
    }

    public void setList(List<Observer> list) {
        this.list = list;
    }

    public void attach(Observer observer) {
        list.add(observer);
        System.out.println("增加一个观察者.");
    }

    public void death(Observer observer) {
        list.remove(observer);
        System.out.println("移除一个观察者.");
    }

    /*通知所有观察者,将自身对象作为参数传入，实现最大灵活性*/
    public void notifyObservers() {
        for (Observer observer : list) {
            observer.update(this);
        }
    }
}
